package com.zzk.cloudfile.demos.web.util;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author Administrator
 * @Title: ExecutorUtils
 * @Description:
 * @date 2024/5/3 19:18
 */
public class ExecutorUtils {
    private static final ExecutorService executor = Executors.newCachedThreadPool();

    public static <T> Future<T> createFuture(Callable<T> task) {
        return executor.submit(task);
    }

    public static void waitComplete(List<Future<?>> futures) throws InterruptedException, ExecutionException {
        for (Future<?> future : futures) {
            future.get();
        }
    }

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        List<Future<?>> futures = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Future<Integer> future = createFuture(() -> {
                System.out.println("Task started");
                Thread.sleep(1000);
                System.out.println("Task finished");
                return 42;
            });
            futures.add(future);
        }

        waitComplete(futures);
//        executor.shutdown();
    }
}
